/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.TablasEquivalencias;

import BD.BaseDatos;
import com.myapp.struts.Carrera.Carrera;
import java.sql.ResultSet;
import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Andreina Wilhelm
 */
public class SeleccionarListaCarrerasAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    LinkedList<Carrera> crearLista(BaseDatos bd, String idInstitucion) {
        // se crea lista
        LinkedList<Carrera> carreraList = new LinkedList<Carrera>();

        try {
            // se crea el query
            String query = "SELECT * FROM carrera WHERE activo='true' AND idInstitucion='" + idInstitucion + "'ORDER BY nombre";

            // se ejecuta query
            ResultSet rs = bd.ejecutarQuery(query);

            while (rs.next()) {
                Carrera car = new Carrera(rs.getString("codigo"), rs.getString("nombre"), Integer.parseInt(idInstitucion), rs.getString("tipo"));
                carreraList.add(car);
            }

        } catch (Exception e) {
            System.out.println("ERROR: Problema al consultar la BD");
        }

        return carreraList;
    }

    
    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // crea instancia de BD
        BaseDatos bd = new BaseDatos();

        SeleccionarListaCarrerasForm slcf = (SeleccionarListaCarrerasForm) form;
        String idInstOrig = slcf.getID_InstOrigen();
        String idInstDest = slcf.getID_InstDestino();

        try {
            // establece conexion con BD
            bd.establecerConexion();

            // crear lista de carrera de origen
            slcf.setLista_CarrerasOrigen(crearLista(bd, idInstOrig));

            // crear lista de carrera de destino
            slcf.setLista_CarrerasDestino(crearLista(bd, idInstDest));
            
            // Se termina conexion con BD
            bd.terminarConexion();
           
                        
            // Al cambiar de institucion las listas de las materias y tablas deben cambiar entonces elimino esos forms de la sesion
            HttpSession session = request.getSession();
            session.removeAttribute("SeleccionarListaMateriasForm");
            session.removeAttribute("SeleccionarListaTablasEquivalenciaForm"); 
            
            return mapping.findForward(SUCCESS);

        } catch (Exception e) {
            System.out.println("ERROR: Problema al consultar la BD");
        }

        // Se termina conexion con BD
        bd.terminarConexion();
            
        return mapping.findForward(FAILURE);
    }
}
